export const subjectMap = {
	10: {
		name: '单选题',
		type: 'u-radio',
		setPayload(subject) {
			return {
				value: null
			}
		},
		setOptions(option) {
			option.input = !!option.ALLOW_INPUT
			option.placeholder = option.input ? option.OPTION_DESC || '请输入' : option.OPTION_DESC
			option.value = ''
		},
		getSubjectValue(subject) {
			// 单选比较特殊, 有可能存在input情况
			const option = subject.options.find(o => o.ID == subject.payload.value && o.citeShow && o.optionAssShow)
			if (!option) return {
				value: false,
				subject
			}

			if (option.input) {
				if (!option.value) return {
					value: false,
					subject
				}
				return {
					value: {
						subject_id: subject.SUBJECT_ID,
						option1: subject.payload.value,
						answer_content: option.value
					},
					subject
				}
			} else {
				return {
					value: {
						subject_id: subject.SUBJECT_ID,
						option1: subject.payload.value
					},
					subject
				}
			}
		}
	},
	11: {
		name: '多选题',
		type: 'u-checkbox',
		setPayload(subject) {
			return {
				value: []
			}
		},
		setOptions(option) {
			option.input = !!option.ALLOW_INPUT
			option.placeholder = option.input ? option.OPTION_DESC || '请输入' : option.OPTION_DESC
			option.value = ''
		},
		setCheckboxMax(subject) {
			// 多选有最多选项限制
			subject.options.forEach(o => o.disabled = false)
		},
		getSubjectValue(subject) {
			// 多选比较特殊, 有可能存在input情况
			const list = subject.options.filter(o => subject.payload.value.includes(o.ID) && o.citeShow && o
				.optionAssShow)
			if (!list.length) return {
				value: false,
				subject
			}
			const inputList = list.filter(item => item.input)
			if (!inputList.length) {
				return {
					value: list.map(item => {
						return {
							subject_id: subject.SUBJECT_ID,
							option1: item.ID,
						}
					}),
					subject
				}
			} else {
				if (inputList.some(item => !item.value)) return {
					value: false,
					subject
				}
				return {
					value: list.map(item => {
						if (item.input) return {
							subject_id: subject.SUBJECT_ID,
							option1: item.ID,
							answer_content: item.value
						}
						return {
							subject_id: subject.SUBJECT_ID,
							option1: item.ID,
						}
					}),
					subject
				}
			}
		}
	},
	25: {
		name: '下拉单选',
		type: 'u-picker',
		setPayload(subject) {
			return {
				value: '',
				text: ''
			}
		},
		getSubjectValue(subject) {
			if (subject.payload.value) {
				return {
					value: {
						subject_id: subject.SUBJECT_ID,
						option1: subject.payload.value
					},
					subject
				}
			}
			return {
				value: false,
				subject
			}
		}
	},
	12: {
		name: '填空题',
		type: 'u-input',
		setPayload(subject) {
			return {
				value: ''
			}
		},
		getSubjectValue(subject) {
			if (subject.payload.value && subject.OPTION_MIN && subject.OPTION_MIN <= subject.payload.value.length ) {
				return {
					value: {
						subject_id: subject.SUBJECT_ID,
						answer_content: subject.payload.value
					},
					subject
				}
			} else if (subject.payload.value && !subject.OPTION_MIN) {
				return {
					value: {
						subject_id: subject.SUBJECT_ID,
						answer_content: subject.payload.value
					},
					subject
				}
			}
			return {
				value: false,
				subject
			}
		}
	},
	13: {
		name: '图片文件',
		type: 'u-image',
		setPayload(subject) {
			return {
				value: [],
				examPic: []
			}
		},
		getSubjectValue(subject) {
			if (subject.payload.examPic.length > 0 && subject.OPTION_MIN <= subject.payload.examPic.length) {
				return {
					value: {
						subject_id: subject.SUBJECT_ID,
						answer_content: subject.payload.examPic.join(',')
					},
					subject
				}
			}
			return {
				value: false,
				subject
			}
		}
	},
	26: {
		name: '滑动条',
		type: 'u-slider',
		setPayload(subject) {
			return {
				value: 0,
			}
		},
		setTicks(subject) {
			const ticks = []
			const step = (subject.OPTION_MAX - subject.OPTION_MIN) / 10
			const tickNum = step
			for (let i = 0; i <= tickNum; i++) {
				ticks.push({
					step: i * step,
					active: false,
					left: i * step,
					showNum: (i * step % 20) == 0
				})
			}
			return ticks
		},
		getSubjectValue(subject) {
			if (subject.payload.value) {
				return {
					value: {
						subject_id: subject.SUBJECT_ID,
						answer_content: subject.payload.value
					},
					subject
				}
			}
			return {
				value: false,
				subject
			}
		}
	},
	27: {
		name: '量表/NFS',
		type: 'nfs',
		setPayload(subject) {
			return {
				value: 0
			}
		},
		setNfs(subject) {
			const nfs = []
			const step = (subject.OPTION_MAX - subject.OPTION_MIN)
			for (let i = 0; i <= step; i++) {
				nfs.push({
					i,
					active: false
				})
			}
			return nfs
		},
		getSubjectValue(subject) {
			if (subject.payload.value) {
				return {
					value: {
						subject_id: subject.SUBJECT_ID,
						answer_content: subject.payload.value
					},
					subject
				}
			}
			return {
				value: false,
				subject
			}
		}
	},
	9: {
		name: '地理位置',
		type: 'location',
		setPayload(subject) {
			return {
				value: '',
				address: '',
				address_detail: ''
			}
		},
		getSubjectValue(subject) {
			if (subject.payload.address && subject.payload.address_detail) {
				return {
					value: {
						subject_id: subject.SUBJECT_ID,
						answer_content: JSON.stringify({
							address: subject.payload.address,
							address_detail: subject.payload.address_detail
						})
					},
					subject
				}
			}
			return {
				value: false,
				subject
			}
		}
	},
	29: {
		name: '打分星星',
		type: 'star',
		setPayload(subject) {
			return {
				value: 0,
			}
		},
		getSubjectValue(subject) {
			if (subject.payload.value) {
				return {
					value: {
						subject_id: subject.SUBJECT_ID,
						answer_content: subject.payload.value
					},
					subject
				}
			}
			return {
				value: false,
				subject
			}
		}
	},
	8: {
		name: '日期时间',
		type: 'u-datetime',
		setPayload(subject) {
			return {
				value: null,
				datetime: Number(new Date())
			}
		},
		getSubjectValue(subject) {
			if (subject.payload.value) {
				return {
					value: {
						subject_id: subject.SUBJECT_ID,
						answer_content: subject.payload.value
					},
					subject
				}
			}
			return {
				value: false,
				subject
			}
		}
	},
	20: {
		name: '矩阵单选题',
		type: 'matrix-radio',
		setPayload(subject) {
			const obj = {}
			subject.options.forEach(p => {
				obj[p.ID] = {
					value: ''
				}
			})
			return obj
		},
		getSubjectValue(subject) {
			const options = subject.options.filter(option => option.citeShow)
			if (!options.length) return {
				value: false,
				subject
			}
			if (options.every(item => subject.payload[item.ID].value)) {
				return {
					value: options.map(item => {
						return {
							subject_id: subject.SUBJECT_ID,
							option1: item.ID,
							option2: subject.payload[item.ID].value
						}
					}),
					subject
				}
			}
			return {
				value: false,
				subject
			}
		}
	},
	30: {
		name: '量表/NFS',
		type: 'matrix-nfs',
		setPayload(subject) {
			const obj = {}
			subject.options.forEach(p => {
				obj[p.ID] = {
					value: null
				}
			})
			return obj
		},
		setMatrixNfs(subject) {
			subject.options.forEach(p => {
				const nfs = []
				const step = (subject.OPTION_MAX - subject.OPTION_MIN)
				for (let i = 0; i <= step; i++) {
					nfs.push({
						i,
						active: false
					})
				}
				p.nfs = nfs
			})
		},
		getSubjectValue(subject) {
			const options = subject.options.filter(option => option.citeShow)
			if (!options.length) return {
				value: false,
				subject
			}
			if (options.every(item => {
					return subject.payload[item.ID].value != null
				})) {
				return {
					value: options.map(item => {
						return {
							subject_id: subject.SUBJECT_ID,
							option1: item.ID,
							answer_content: subject.payload[item.ID].value
						}
					}),
					subject
				}
			}
			return {
				value: false,
				subject
			}
		}
	},
	21: {
		name: '矩阵多选题',
		type: 'matrix-checkbox',
		setPayload(subject) {
			const obj = {}
			subject.options.forEach(p => {
				obj[p.ID] = {
					value: []
				}
			})
			return obj
		},
		getSubjectValue(subject) {
			const options = subject.options.filter(option => option.citeShow)
			if (!options.length) return {
				value: false,
				subject
			}
			if (options.every(item => subject.payload[item.ID].value.length)) {
				const list = []
				const arr = options.map(item => {
					return {
						subject_id: subject.SUBJECT_ID,
						option1: item.ID,
						option2: subject.payload[item.ID].value
					}
				})
				arr.forEach(item => {
					item.option2.forEach(op2 => {
						list.push({
							subject_id: subject.SUBJECT_ID,
							option1: item.option1,
							option2: op2
						})
					})
				})
				return {
					value: list,
					subject
				}
			}
			return {
				value: false,
				subject
			}
		}
	},
	31: {
		name: '打分星星',
		type: 'matrix-star',
		setPayload(subject) {
			const obj = {}
			subject.options.forEach(p => {
				obj[p.ID] = {
					value: 0
				}
			})
			return obj
		},
		getSubjectValue(subject) {
			const options = subject.options.filter(option => option.citeShow)
			if (!options.length) return {
				value: false,
				subject
			}
			if (options.every(item => subject.payload[item.ID].value)) {
				return {
					value: options.map(item => {
						return {
							subject_id: subject.SUBJECT_ID,
							option1: item.ID,
							answer_content: subject.payload[item.ID].value
						}
					}),
					subject
				}
			}
			return {
				value: false,
				subject
			}
		}
	},
	40: {
		name: '多维打分',
		type: 'multple-score',
		setPayload(subject) {
			return {
				// 打分
				value: 0
			}
		},
		setMultpleScore(subject) {
			subject.options.forEach(o => {
				o.options = subject.options3.filter(o3 => o3.OPTION1_SNO == o.SNO)
				o.value = null
				if (o.ADD_REASON_SCORE) {
					o.reasonValue = ''
					o.showReasonValue = false
				}
			})
		},
		getSubjectValue(subject) {
			if (subject.options.find(o => !o.value)) {
				return {
					value: false,
					subject
				}
			}
			const list = []

			subject.options.forEach(o => {
				const index = o.options.findIndex(option => option.ID == o.value)
				const obj = {
					subject_id: subject.SUBJECT_ID,
					subject_sno: subject.SUBJECT_SNO,
					option1: o.ID,
					option1_sno: o.SNO,
					option2: subject.options2[index].ID,
					option2_sno: subject.options2[index].SNO,
					answer_content: subject.options2[index].SNO
				}
				if (o.showReasonValue && o.reasonValue) {
					obj.option_reason = o.reasonValue
				}
				list.push(obj)
			})
			return {
				value: list,
				subject
			}
		}
	}
}

function shuffleArray(arr) {
	for (let i = arr.length - 1; i > 0; i--) {
		const j = Math.floor(Math.random() * (i + 1));
		[arr[i], arr[j]] = [arr[j], arr[i]];
	}
	return arr;
}


export function partiallyShuffleArray(arr, numRandomElements) {
	if (numRandomElements == -1) {
		return arr
	}
	let length = arr.length
	if (numRandomElements != -2) {
		length = length - numRandomElements
	}
	const indexList = shuffleArray(Array.from({
		length
	}).map((_, index) => index))
	const newArr = []
	indexList.forEach(index => {
		newArr.push(arr[index])
	})
	let finalArr
	if (numRandomElements != -2) {
		finalArr = newArr.concat(arr.slice(arr.length - numRandomElements))
	} else {
		finalArr = newArr
	}
	return finalArr
}